package com.zxkj.service;

import com.zxkj.domain.entity.HighlyCitedDoc;
import com.zxkj.domain.entity.HotCitedDoc;
import com.zxkj.domain.mapper.HighlyCitedDocRepository;
import com.zxkj.domain.mapper.HotCitedDocRepository;
import com.zxkj.solr.model.SciDocSolrModel;
import com.zxkj.solr.repository.SciDocSolrRepository;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.SimpleStringCriteria;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service
public class CitedService {
    private static final Logger logger = LoggerFactory.getLogger(FixServiceImpl.class.getName());
    @Autowired
    private HighlyCitedDocRepository highlyCitedDocRepository;
    @Autowired
    private HotCitedDocRepository hotCitedDocRepository;
    @Autowired
    private SolrTemplate solrTemplate;
    @Value("${solr.core.name}")
    private String solrCoreName;
    @Value("${highly_cited_year}")
    private Integer highly_cited_year;
    @Value("${highly_cited_month}")
    private Integer highly_cited_month;

    /**
     * 更新solr的高被引和热点字段
     */
    public void updateSolr(Integer startPage, Integer endPage) {
        while (true) {
            if (startPage > endPage) {
                return;
            }
            logger.warn("第" + startPage + "页");
            SimpleQuery query = new SimpleQuery(new SimpleStringCriteria("*:*"), new PageRequest(startPage, 1000));
            Page<SciDocSolrModel> page = solrTemplate.queryForPage(solrCoreName, query, SciDocSolrModel.class);
            SolrClient solrClient = solrTemplate.getSolrClient();
            if (page.getContent().size() == 0) {
                break;
            }
            for (SciDocSolrModel sciDocSolrModel : page.getContent()) {
                updateByUt(sciDocSolrModel.getUt(), sciDocSolrModel.getId(), solrClient);
            }
            try {
                solrClient.commit(solrCoreName);
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            startPage++;
        }
    }


    private void updateByUt(String ut, String id, SolrClient solrClient) {
        HighlyCitedDoc highlyCitedDoc = highlyCitedDocRepository.findByUtAndYearAndMonth(ut, highly_cited_year, highly_cited_month);
        SolrInputDocument doc = new SolrInputDocument();
        Map<String, String> partialUpdate = new HashMap<>();
        partialUpdate.put("set", highlyCitedDoc == null ? null : highlyCitedDoc.getResearch_area());
        doc.addField("ID", id);
        doc.addField("HighlyCited", partialUpdate);
        try {
            solrClient.add(solrCoreName, doc);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        HotCitedDoc hotCitedDoc = hotCitedDocRepository.findByUtAndYearAndMonth(ut, highly_cited_year, highly_cited_month);
        doc = new SolrInputDocument();
        partialUpdate = new HashMap<>();
        partialUpdate.put("set", hotCitedDoc == null ? null : hotCitedDoc.getResearch_area());
        doc.addField("ID", id);
        doc.addField("HotCited", partialUpdate);
        try {
            solrClient.add(solrCoreName, doc);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
